package Main;

import url.DBUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.sql.SQLException;
import java.util.ArrayList;


public class AppoimentFrame extends JFrame {
    private JTable table;
    private String[] columnNames = {"编号", "名称" , "预约时间"};
    AppoimentFrame() throws SQLException {
        Font f = new Font("宋体", Font.PLAIN, 25);
        // 窗口大小
        setSize(800, 600);
        // 窗口可视化
        setVisible(true);
        // 窗口标题
        setTitle("场地管理系统");
        // yuyue
        setLocationRelativeTo(null);
        // 窗口大小不可改变
        setResizable(false);
        // 窗口的关闭事件
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout());

        DBUtil.initst();
        ArrayList<appoiment> list = new ArrayList<>();
        String sql = "select * from appoiment";
        DBUtil.select(sql);
        while(DBUtil.rs.next()){
            appoiment a = new appoiment();
            a.setNumber(DBUtil.rs.getString(1));
            a.setName(DBUtil.rs.getString(2));
            a.setDate(DBUtil.rs.getString(3));
            list.add(a);
        }
        DBUtil.closeDB();

        // 创建数据数组
        Object [][] data=new Object[list.size()][columnNames.length];
        for(int i = 0; i<list.size(); i++){
            appoiment a = list.get(i);
            data[i][0] = a.getNumber();
            data[i][1] = a.getName();
            data[i][2] = a.getDate();
        }
        // 创建DefaultTableModel并设置列名和数据
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model);
        table.setEnabled(false);
        table.setFont(new Font("宋体", Font.PLAIN, 20));
        table.setBorder(null);
        // 设置行高
        table.setRowHeight(35);
        JTableHeader header = table.getTableHeader();
        // 设置列名的大小
        header.setFont(new Font("宋体", Font.BOLD, 20));
        //设置列表头不可别用户重新拖动排列
        table.getTableHeader().setReorderingAllowed(false);
        // 添加JTable到JScrollPane
        JScrollPane scrollPane = new JScrollPane(table);
        centerPanel.add(scrollPane, BorderLayout.CENTER);

        add(centerPanel);
    }
}
